package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetMealController {


    @Autowired
    private SetMealService setMealService;


    /**
     * 批量起售停售及单个起售停售
     */
    @PostMapping("/status/{type}")
    @CacheEvict(cacheNames = "setmealcache",allEntries = true)
    public R updateStatus(@RequestParam List<Long> ids, @PathVariable Integer type){
        int rows = setMealService.updateStatus(ids,type);
        if (rows >0){
            return R.success("修改起售停售状态成功");
        }else {
            return R.error("修改起售停售状态失败");
        }
    }

    /**
     * 根据分类id和装填来查询套餐数据
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @Cacheable(cacheNames = "setmealcache",key = "#categoryId+'_'+#status")
    public R findByCatergoryId(Long categoryId, Integer status) {
        //调用service
        List<Setmeal> list = setMealService.findCategoryById(categoryId, status);
        return R.success(list);
    }

    /**
     * 删除套餐（包含单个删除和批量删除）
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealcache",allEntries = true)
    public R delete(@RequestParam List<Long> ids) {

        int row = setMealService.delete(ids);
        if (row > 0) {
            return R.success("删除成功!~");

        } else {
            return R.error("删除失败!~");
        }
    }
    /**
     * 分页查询套餐
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){
        IPage<SetmealDto>page=setMealService.findPage(pageParam);

        return R.success(page);
    }

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmealcache",allEntries = true)
    public R add(@RequestBody SetmealDto setmealDto) {

        int row = setMealService.add(setmealDto);

        if (row > 0) {
            return R.success("新增套餐成功");
        } else {
            return R.error("新增套餐失败");
        }
    }

    /**
     * 修改套餐前
     * @param setmealid
     * @return
     */
    @GetMapping
    @Cacheable(cacheNames = "setmealcache",key = "#setmealid")
    public R updata(Long setmealid) {

        List<SetmealDish> list = setMealService.updata(setmealid);

        return R.success(list);
    }
    /**
     * 修改套餐确认后
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmealcache",allEntries = true)
    public R updataput(@RequestBody SetmealDto setmealDto) {

        int row = setMealService.updataput(setmealDto);

        if (row > 0) {
            return R.success("修改套餐成功");
        } else {
            return R.error("修改套餐失败");
        }
    }
}
